/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-08-22
 * Time: 11:21
 */
public class Test {
    //leetcode 34 在排序数组中查找元素的第一个和最后一个位置
    public int[] searchRange(int[] nums, int target) {
        int[] ret = new int[]{-1,-1};
        int n = nums.length;
        if(n == 0) return ret;
        int left = 0;
        int right = n-1;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        if(nums[left] == target) {
            ret[0] = left;
        }
        right = n - 1;
        while(left < right) {
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] <= target) {
                left = mid;
            }else {
                right = mid - 1;
            }
        }
        if(nums[left] == target) {
            ret[1] = left;
        }

        return ret;
    }

    //leetcode 69 x的平方根
    public int mySqrt(int x) {
        long left = 0;
        long right = x;
        while(left < right) {
            long mid = left + (right - left + 1) / 2;
            if(mid * mid > x) {
                right = mid - 1;
            }else {
                left = mid;
            }
        }

        return (int)left;
    }
}
